package cn.itcast.controller;

import cn.itcast.domain.Account;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  请求参数绑定
 */
@Controller
@RequestMapping("/param")
public class ParamController {

    /**
     * 请求参数绑定
     *      规则: 方法参数名与请求参数名一致既可以自动绑定,使用了反射完成
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/testParam")
    public String testParam(String username, String password) {
        System.out.println("执行了...");
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        return "success";
    }

    /**
     *  请求参数绑定把数据封装到JavaBean的类 List/Map集合中中
     *      javabean: 采用 类的变量名 = 请求参数名
     *      javabean中的类: 采用 类的变量名.子类变量名
     *      List: 变量名[0]
     *      Map: 变量名滚[键值]   简直一般为字符串 'one'
     * @param account
     * @return
     */
    @RequestMapping("/saveAccount")
    public String saveAccount(Account account) {
        System.out.println("执行了...");
        System.out.println(account);
        return "success";
    }

    /**
     *  自定义类型转换
     * @param user
     * @return
     */
    @RequestMapping("/saveUser")
    public String saveUser(User user) {
        System.out.println("执行了...");
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/testServlet")
    public String testServlet(HttpServletRequest  request, HttpServletResponse response) {
        System.out.println("执行了...");
        System.out.println(request);
        System.out.println(response);

        HttpSession session = request.getSession();
        System.out.println(session);

        ServletContext servletContext = session.getServletContext();
        System.out.println(servletContext);

        return "success";
    }
}
